﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using XRM.Security.DataAccess.Helpers;
using XRM.Security.DataAccess.Models;

namespace XRM.Security.WebUI.Models.UserRegister
{
    public class UserRegisterAddPCModel : PCModel, IValidatableObject
    {
        public int UserRegisterId { get; set; }

        public UserRegisterAddPCModel()
        {   
        }

        public UserRegisterAddPCModel(PC dbEntity) : base(dbEntity)
        {}

        public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
        {
            StickerList = StickerList.Where( x => !string.IsNullOrWhiteSpace( x.StickerNumber ) ).ToList();
            if ( StickerList.Select( x => x.StickerNumber ).Distinct().Count() != StickerList.Count() )
            {
                yield return new ValidationResult( "Номера наклеек не уникальны", new[] { "StickerList" } );
            }
            if ( Controller.PCRepository.GetAll( x => x.SerialNumber == SerialNumber && x.PCId != Id ).Actual().Any() )
            {
                yield return new ValidationResult( string.Format( "ПК с серийным номером '{0}' уже существует", SerialNumber ), new[] { "SerialNumber" } );
            }
        }
    }

    public class UserRegisterPCEditModel : BaseViewModel, IValidatableObject
    {
        public UserRegisterPCEditModel()
        {
            PCList = new List<PCModel>();
        }

        public void GetUserRegister( DataAccess.Models.UserRegister dbEntity )
        {
            UserRegister = new UserRegisterModel( dbEntity );
        }

        public int UserRegisterId { get; set; }
        public UserRegisterModel UserRegister { get; set; }

        public List<PCModel> PCList { get; set; }

        public IEnumerable<ValidationResult> Validate( ValidationContext validationContext )
        {
            yield break;
        }
    }
}